(function(){
    _$(document).ready(function(){
        function app(){};
        app.prototype.ajaxFn=function(){
            _$.ajaxSetup({
                async : false
            });
            _$.post('/getbookInfo',{bid:bid},function(data){
                console.log(data);
                if(data['code']==1){
                    var info=data['info']['bookInfo'];
                    _$("#title").html(info['bName']);
                    _$("title").html(info['bName']+"_"+info['bAuth']+"著_异世大陆小说_"+info['bName']+"txt下载_"+info['bName']+"无弹窗_起点中文网");
                    _$("#meta1").html(info['bName']+"官方首发,"+info['bName']+"小说,"+info['bName']+"最新章节,"+info['bName']+"txt下载,"+info['bName']+"无弹窗,超级浮空城吧,"+info['bName']+"离线完本");
                    _$("#meta2").html(info['bName']+","+info['bName']+"小说阅读,异世大陆小说"+info['bName']+"由作家"+info['bAuth']+"创作,起点中文网提供"+info['bName']+"首发最新章节及txt下载,"+info['bName']+"最新更新章节,更多精彩尽在起点中文网。");
                    _$("#aid").val(info['cid']);
                    _$("#aName").val(info['bAuth']);
                    _$("#bName").val(info['bName']);
                    _$("#imgurl").val(info['imgurl']);
                }
            });
            _$.post('/getChapter',{bid:bid,seq:true},function(data){
                if(data['code']==1){
                    if(data['info'].length>0){
                        var info=data['info'];
                        for(var val in info){
                            //console.log(brinfo[val]);
                            info[val]['updateTime']=getTimes(info[val]['updateTime']);
                            var html = template('tpl', info[val]);
                            _$("#volumes").append(html);
                        }
                    }else{
                        _$("#volumes").html('<div class="null-x"><div class="null">暂无章节</div></div>');
                    }
                    _$("#chapterall").html(data['info'].length);
                }
            })
        };
        app.prototype.eventFn=function(){
            _$("#reverse").click(function(){
                var key=_$(this).attr('data-key');
                if(key=='1'){
                    _$("#volumes").html("");
                    _$.post('/getChapter',{bid:bid,seq:false},function(data){
                        if(data['code']==1){
                            if(data['info'].length>0){
                                var info=data['info'];
                                for(var val in info){
                                    //console.log(brinfo[val]);
                                    info[val]['updateTime']=getTimes(info[val]['updateTime']);
                                    var html = template('tpl', info[val]);
                                    _$("#volumes").append(html);
                                    clickList();
                                }
                            }else{
                                _$("#volumes").html('<div class="null-x"><div class="null">暂无章节</div></div>');
                            }
                        }
                    });
                    _$(this).html('顺序').attr('data-key','0');
                }else if(key=='0'){
                    _$("#volumes").html("");
                    _$.post('/getChapter',{bid:bid,seq:true},function(data){
                        if(data['code']==1){
                            if(data['info'].length>0){
                                var info=data['info'];
                                for(var val in info){
                                    //console.log(brinfo[val]);
                                    info[val]['updateTime']=getTimes(info[val]['updateTime']);
                                    var html = template('tpl', info[val]);
                                    _$("#volumes").append(html);
                                    clickList();
                                }
                            }else{
                                _$("#volumes").html('<div class="null-x"><div class="null">暂无章节</div></div>');
                            }
                        }
                    });
                    _$(this).html('倒序').attr('data-key','1');
                }
            });
            function clickList(){
            _$(".chapter-li").click(function(){
                var taht=_$(this);
                if(session){
                    _$.post('/setBookChapter',{
                        bid:bid,
                        cid:taht.attr('data-chapter-id'),
                        cName:taht.attr('data-chapter-name')
                    },function(data){
                        console.log(data);
                        location.href="/look/"+bid+"?chapter="+taht.attr('data-chapter-id');
                    })
                }else{
                    var localRead=localStorage.getItem('OLD_READ');
                    var readobj={
                        bid:bid,
                        bName:_$("#bName").val(),
                        aid:_$("#aid").val(),
                        aName:_$("#aName").val(),
                        cid:taht.attr('data-chapter-id'),
                        cName:taht.attr('data-chapter-name'),
                        time:new Date().getTime(),
                        imgurl:_$("#imgurl").val()
                    };
                    if(localRead==""||localRead==null||localRead==undefined){
                        localStorage.setItem('OLD_READ',JSON.stringify([readobj]));
                    }else{
                        var bidkey=true;
                        var localReadObj=JSON.parse(localRead);
                        for(var val in localReadObj){
                            if(localReadObj[val]['bid']==bid){
                                localReadObj[val]['bName']=$("#bName").val();
                                localReadObj[val]['aid']=$("#aid").val();
                                localReadObj[val]['aName']=$("#aName").val();
                                localReadObj[val]['cid']=taht.attr('data-chapter-id');
                                localReadObj[val]['cName']=taht.attr('data-chapter-name');
                                localReadObj[val]['time']=new Date().getTime();
                                localReadObj[val]['imgurl']=_$("#imgurl").val();
                                bidkey=false;
                                break;
                            }
                        }
                        if(bidkey){
                            localReadObj.push(readobj);
                        }
                        localStorage.setItem('OLD_READ',JSON.stringify(localReadObj));
                        location.href="/look/"+bid+"?chapter="+taht.attr('data-chapter-id');
                    }
                }
            });
            }
            clickList();
        };
        app.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new app().init();
    })
})();
